<template>
<!-- 打款审核新增页面弹框 -->
  <div>
    <div style="height:300px">
      <FormPanel
        ref="from"
        :list="initForm"
        :formType="actionType"
        :cols="3"
        :isSubmitBtn="false"
        :collapse="false"
      />
    </div>
    <div class="dialogBottmView">
      <el-button :loading="loadingFlag" @click="beforeSave()" type="primary">保 存</el-button>
      <el-button @click="closeDialog('closeDialog')">返 回</el-button>
    </div>
    
  <BaseDialog
    :visible.sync="issearch"
    :title="'服务站查询'"
    destroyOnClose
    :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
  >
    <ownerSeacher  @close="dealercloseHandler" />
  </BaseDialog>
  </div>
</template>
<script>

import { dictionary } from '@/utils/dictMixin';
import ownerSeacher from '@/pages/partsmanage/claim/approve/ownerSeacher';
import { plus } from '@/utils/validate';
import { addOrEditBill} from '@/api/partsmanage/financialManagement/capitalAccount/index';
import { getFileGroup } from '@/api/partsmanage/common/file';
import config from '@/assets/js/config';
export default {
  props: {
    row: {
      type: Object,
      required: false,
      default: null
    },
    actionType:{
      type: String,
      required: true,
      default: null
    }
  },
  components: {ownerSeacher},
  data(){
    return{
      issearch:false,
      initForm:this.createdFrom(),
      loadingFlag:false,
      dealerName:""
    }
  },
  mixins:[dictionary],
  created() {
    if(this.actionType  == "edit"){
      this.init()
    }
  },
  methods:{
    init(){
      getFileGroup({ "xtdjid": this.row.id==null?-1:this.row.id, "xtfjlx": "tt_dealer_account_adjust" }).then(response => {
        var fileList = response.data.records
        this.initForm.forEach(x => {
          if (x.fieldName === 'uploadFile') {
            x.initialValue = fileList
          }else{
            x.initialValue = this.row[x.fieldName];
          }
        })
      })
    },
    save(data){
      addOrEditBill(data).then(result =>{
        this.$notify.success({title: '提示信息',message:'保存成功!'});
        this.loadingFlag = false;
        this.closeDialog('refresh');

      }).catch(err =>{

        this.$notify.error({title: '提示信息',message:err||'保存失败!'});
      })
    },
    async beforeSave(){//保存
      this.loadingFlag = true;
      let [err, data] = await this.$refs.from.GET_FORM_DATA();
      if(err){
        this.loadingFlag = false; 
        return;
      } 
      data.mark = this.actionType;
      if("edit" == this.actionType){
        data.id = this.row.id;
      }else{
        data.dealerName = this.dealerName;
      }
      if(data.uploadFile == undefined || data.uploadFile == null || data.uploadFile.length <=0){
        this.$confirm('当前上传的附件为空或格式不对,保存后附件可能失效,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() =>{
          this.save(data);
        }).catch(() =>{
          this.loadingFlag = false;
        })
      }else{
        this.save(data);
      }
    },
    closeDialog(val){//关闭弹框
      this.$emit('close')
    },
    dealercloseHandler(val){
      if(val){
        this.initForm.find(x=>x.fieldName=='dealerCode').initialValue=val.dealerCode;
        this.dealerName=val.dealerName;
      }
      this.issearch=false;
    },
    seacherFun(){
      this.issearch = true
    },
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '服务站查询',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          disabled:this.row == null ? false : true,
          maxlength:30,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          },
          rules: [
            {
              required: true,
              message: '请选择服务站',
              trigger: 'blur'
            }
          ]
        },
        {
            type: 'SELECT',
            label: '往来账类别',
            fieldName: 'inOutType',
            placeholder: '请选择',
            itemList: this.createDictList('3021'),
            rules: [
                {
                  required: true,
                  message: '请选择往来账类别',
                  trigger: 'blur'
                }
            ]
        },
        {
            type: 'SELECT',
            label: '记账类别',
            fieldName: 'adjustType',
            placeholder: '请选择',
            itemList: this.createDictList('3022'),
            rules: [
                {
                  required: true,
                  message: '请选择记账类别',
                  trigger: 'blur'
                }
            ]
        },
        {
            type: 'SELECT',
            label: '记账方式  ',
            fieldName: 'adjustWay',
            placeholder: '请选择',
            itemList: this.createDictList('3023'),
            initialValue:30231001,
            disabled:true,
            rules: [
                {
                  required: true,
                  message: '请选择记账方式',
                  trigger: 'blur'
                }
            ]
        },
        {
          type: 'INPUT',
          label: '记账凭证号',
          fieldName: 'adjustNo',
          placeholder: '请输入',
          rules: [
            {
              required: true,
              message: '请输入记账凭证号',
              trigger: 'blur'
            }
          ]
        },
        {
            type: 'INPUT_NUMBER',
            label: '金额',
            fieldName: 'amount',
            precision:2,
            rules: [
                { 
                    required: true,
                    message: '请输入金额', 
                    trigger: 'blur' 
                },
                {
                    validator: plus, 
                    message: '金额格式不正确', 
                    trigger: 'blur' 
                }
            ],
        },
        {
            type: 'TEXT_AREA',
            label: '备注',
            fieldName: 'remark',
            placeholder: '请输入',
            selfCols:3,
            maxlength:150,
            rows:3
        },
        {
            selfCols: 3,
            type: 'UPLOAD_FILE',
            label: '上传文件',
            fieldName: 'uploadFile',
            placeholder: '上传文件...',
            initialValue: '',
            upload: {
                actionUrl: `${config.uploadUrl}/api/file/oss/upload`,
                limit: 1,
                tipText: '（支持：jpg、jpeg、png格式）',
                fileTypes:['jpg', 'jpeg', 'png']
            }
        }
      ]
    }
  },
}
</script>
